<?php

if (!function_exists("getElapsedTime")) {
    /**
     * 获取程序运行时间
     *
     * @param int $precision
     * @return string
     * create_at: 2023-08-24 13:36:48
     * update_at: 2023-08-24 13:36:48
     */
    function getElapsedTime($start, int $precision = 2)
    {
        return number_format(microtime(true) - $start, $precision);
    }
}

if (!function_exists("getMemoryUsage")) {
    /**
     * 获取程序运行内存
     *
     * @param int $decimals
     * @return string
     * create_at: 2023-08-24 13:36:48
     * update_at: 2023-08-24 13:36:48
     */
    function getMemoryUsage($size, $precision = 2)
    {
        if (empty($size)) {
            return "0 KB";
        }

        $unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
        return round($size / pow(1024, ($i = floor(log($size, 1024)))), $precision) . ' ' . $unit[$i];
    }
}
